#include "sysfs.h"
#include "sysarch.h"
#include "sysapi.h"
#include "board.h"
#include "sysdebug.h"
#include "Monitor.h"


#include "MenuIf.h"
#include "auto360.h"
#include "video.h"

void fs_init()
{

}


void InitTarget()
{
    SysInit();
    fs_init();
    if (InitDir() == false)
    {
       	printf("create dir fail.\r\n");
        return;
    }

    //InitHiTarget();

    InitDebug();
}


TThreadRet WdgThread(void* pvPara)
{
    int iMonitorID;
	BYTE i = 0;

	ClearWDG();

    while (1)
    {
        Sleep(100);
        ClearWDG();

        i++;
        if (i >= 10)
        {
			i = 0;

            iMonitorID = RunMonitor();  //线程监控,每秒执行一次
            if (iMonitorID < 0)
            {
                iMonitorID = -iMonitorID - 1;

                ResetApp();
                while (1)
                {
                	//等待复位
                }
            }
        }
    }

    return THREAD_RET_OK;
}



//主线程
TThreadRet MainThread(void* pvPara)
{
    if (InitDir() == false)
    {
        printf("########Create dir fail.\r\n");
        return THREAD_RET_ERR;
    }

    SysInitDebug();
    InitDrivers(BOARD_HI3520D);
    InitTarget();

    InitMonitor(); //初始化线程监控
    int iMonitorID = ReqMonitor((char *)"main-thrd", 60 * 60);    //申请线程监控ID,更新间隔为10分钟

	Debug(DEBUG_CRITICAL, ("MainTread: Version v1.0\n"));
    NewThread(WdgThread, NULL, 2048, THREAD_PRIORITY_ABOVE_NORMAL);

    NewThread(Auto360Thread, NULL, 128*1024, THREAD_PRIORITY_ABOVE_NORMAL);
    NewThread(VideoThread, NULL, 8*1024, THREAD_PRIORITY_ABOVE_NORMAL);
    //NewThread(MenuThread, NULL, 8*1024, THREAD_PRIORITY_ABOVE_NORMAL);
    NewThread(DisplayThread, NULL, 8*1024, THREAD_PRIORITY_ABOVE_NORMAL);
    while (1)
	{
    	ClrMonitorWdg(iMonitorID);
    	Sleep(2000);
    }

	SysCleanup();
	return THREAD_RET_OK;
}


